from datetime import datetime
from typing import Union
import time

class TimeHelper:
    """时间处理工具类"""
    
    # 常用时间格式
    FORMAT_FULL = "%Y-%m-%d %H:%M:%S"
    
    @staticmethod
    def now(format: str = FORMAT_FULL) -> str:
        """
        获取当前时间字符串
        :param format: 时间格式
        :return: 格式化的时间字符串
        """
        return datetime.now().strftime(format)
    
    @staticmethod
    def timestamp() -> int:
        """
        获取当前时间戳(秒)
        :return: 时间戳
        """
        return int(time.time())
    
    @staticmethod
    def milliseconds() -> int:
        """
        获取当前时间戳(毫秒)
        :return: 毫秒时间戳
        """
        return int(time.time() * 1000)
    
    @staticmethod
    def format_timestamp(timestamp: Union[int, float], format: str = FORMAT_FULL) -> str:
        """
        时间戳转字符串
        :param timestamp: 时间戳(秒)
        :param format: 目标格式
        :return: 格式化的时间字符串
        """
        return datetime.fromtimestamp(timestamp).strftime(format)
    
    @staticmethod
    def str_to_timestamp(date_str: str, format: str = FORMAT_FULL) -> int:
        """
        字符串转时间戳
        :param date_str: 时间字符串
        :param format: 源格式
        :return: 时间戳(秒)
        """
        return int(datetime.strptime(date_str, format).timestamp())
    
